package net.oschina.allchat.protocol.core.stanza.error;

import net.oschina.allchat.protocol.core.IError;
import net.oschina.allchat.protocol.core.LangText;
import net.oschina.allchat.protocol.core.Protocol;
import net.oschina.allchat.protocol.core.stanza.Stanza;

public class StanzaError extends Stanza implements IError {
	public enum Kind {
		PRESENCE,
		MESSAGE,
		IQ
	}
	
	public enum Type {
		CANCEL,
		CONTINUE,
		MODIFY,
		AUTH,
		WAIT
	}
	
	public static final Protocol PROTOCOL = new Protocol("error");
	public static final String NAMESPACE_STANZA_ERROR_CONTEXT = "chat:stanzas";
	public static final Protocol PROTOCOL_ERROR_DEFINED_CONDITION = new Protocol("*", NAMESPACE_STANZA_ERROR_CONTEXT);
	
	protected StanzaError.Type type;
	protected String definedCondition;
	protected LangText text;
	protected Object ApplicationSpecificCondition;
	protected String senderMessage;
	protected Kind kind;
	
	public StanzaError() {
		this(null);
	}
	
	public StanzaError(StanzaError.Type type) {
		this(type, null);
	}
	
	public StanzaError(StanzaError.Type type, String definedCondition) {
		this(type, definedCondition, null);
	}
	
	public StanzaError(StanzaError.Type type, String definedCondition, LangText text) {
		this.type = type;
		this.definedCondition = definedCondition;
		this.text = text;
	}
	
	// stanza errors are recoverable(rfc3920 9.3)
	@Override
	public boolean closeStream() {
		return false;
	}
	
	@Override
	public String getDefinedCondition() {
		return definedCondition;
	}
	
	@Override
	public LangText getText() {
		return text;
	}
	
	@Override
	public Object getApplicationSpecificCondition() {
		return ApplicationSpecificCondition;
	}
	
	@Override
	public void setApplicationSpecificCondition(Object applicationSpecificCondition) {
		this.ApplicationSpecificCondition = applicationSpecificCondition;
	}
	
	public StanzaError.Type getType() {
		return type;
	}
	
	public void setSenderMessage(String senderMessage) {
		this.senderMessage = senderMessage;
	}
	
	public String getSenderMessage() {
		return senderMessage;
	}
	
	public Kind getKind() {
		return kind;
	}
	
	public void setKind(Kind kind) {
		this.kind = kind;
	}
	
	public void setType(StanzaError.Type type) {
		this.type = type;
	}

	@Override
	public void setDefinedCondition(String definedCondition) {
		this.definedCondition = definedCondition;
	}

	@Override
	public void setText(LangText text) {
		this.text = text;
	}
}
